package cn.wowjoy.pda.android_pharmacy.ui.activity;

import android.os.Bundle;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.app.BaseApplication;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.NetWorkUtils;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityLoginBinding;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.LoginPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.LoginViewModel;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.functions.Consumer;

public class LoginActivity extends BaseActivity<ActivityLoginBinding,LoginViewModel,LoginPresenter> {


    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);
        BaseApplication.finishAllActivity(LoginActivity.class);

        //判断是否免登录
//        if (!TextUtils.isEmpty(PreferenceManager.getInstance().getCurrentUserAccessToken())) {
//            startActivity(new Intent(this, PharmacyActivity.class));
//            finish();
//            findViewById(R.id.image);
//        }
//        binding.tvName.setText("00026");
        binding.tvPsw.setText("123");

//        EditUtils.hideSoftInputMethod(binding.tvName);
        binding.btSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.tvName.getText().length() == 0)
                    new MyToast(LoginActivity.this).showinfo("请输入员工号");
                else if (binding.tvPsw.getText().length() == 0)
                    new MyToast(LoginActivity.this).showinfo("请输入密码");
                else {
                    if(NetWorkUtils.isWifi(LoginActivity.this)){
                        presenter.loadData(binding.tvName.getText().toString().trim(), binding.tvPsw.getText().toString().trim());
                    }else{
                        CreateDialog.failDialog3(LoginActivity.this,"错误","当前网络不可用，请检查你的网络设置");
                    }
                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        RxBus.getInstance().tObservable(Contants.CLEAR_USER_NAME, Integer.class).subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer integer) throws Exception {
                binding.tvName.setText("");
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected Class<LoginPresenter> getPresenterClass() {
        return LoginPresenter.class;
    }

    @Override
    protected Class<LoginViewModel> getViewModelClass() {
        return LoginViewModel.class;
    }
    @Override
    public void onBackPressed() {
        PreferenceManager.getInstance().clearData();
        System.exit(0);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //清除员工信息 和 数据
        PreferenceManager.getInstance().clearData();
    }
}
